package com.zy.pro.order.web;

import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zy.core.model.PageView;
import com.zy.core.web.BaseController;
import com.zy.pro.order.OrderCnt;
import com.zy.pro.order.model.Comment;
import com.zy.pro.order.model.OrderItem;
import com.zy.pro.order.service.CommentService;
import com.zy.pro.order.service.OrderItemService;
import com.zy.pro.order.web.form.CommentSearchForm;
import com.zy.pro.shop.model.Shop;
import com.zy.pro.shop.service.ShopService;
import com.zy.pro.system.web.form.MenuForm;
import com.zy.utils.BigDecimalUtil;
/**
 * 评论管理控制器
 *
 */
@Controller
@RequestMapping("/control/order/commentController")
public class CommentController extends BaseController {

	private static final Logger logger = Logger.getLogger(CommentController.class);
	
	/**
	 * 评论管理服务类
	 */
	@Resource(name = "commentServiceImpl")
	private CommentService commentService;
	
	/**
	 * 订单管理服务类
	 */
	@Resource(name = "orderItemServiceImpl")
	private OrderItemService orderItemService;
	
	/**
	 * 店铺管理服务类
	 */
	@Resource(name = "shopServiceImpl")
	private ShopService shopService;
	
	/**
	 * 用户中心 - 用户对已完成的订单进行评价
	 * @param comment
	 * @param map
	 * @return
	 */
	@RequestMapping("/userAddComment")
	@ResponseBody
	public JSON userAddComment(Comment comment, ModelMap map) {
			JSONObject obj = new JSONObject();
			obj.put("status", "");//状态
			obj.put("info", "");//返回内容
		try {
			OrderItem orItem = orderItemService.find(comment.getCommentOrderId());
			if(orItem != null){
				//设置评论的关联主键(店铺主键,用户主键,案例主键)
				comment.setCommentShopId(orItem.getOrderShopId());
				comment.setCommentUserId(orItem.getOrderUserId());
				if(orItem.getOrderCaseId() != null){
					comment.setCommentCaseId(orItem.getOrderCaseId());
				}
				comment.setCommentCreateTime(new Date());
				//插入评论信息
				commentService.save(comment);
				//更新订单支付状态'已评价'
				OrderItem orderItem = new OrderItem();
				orderItem.setOrderId(comment.getCommentOrderId());
				orderItem.setOrderMoneyStatus(OrderCnt.ORDER_MONEY_COMMENT_STATUS);
				orderItemService.updateBySelective(orderItem);
				
				//更新店铺评价条数
				Shop shop = shopService.find(orItem.getOrderShopId());
				Shop sp = new Shop();
				sp.setShopId(orItem.getOrderShopId());
				sp.setShopCommentNum(shop.getShopCommentNum() + 1);
				
				//设置店铺信誉度
				//(用户最新评论信誉度*0.6 + 旧的评论信誉度)/2
				BigDecimal newCre = BigDecimalUtil.mul(comment.getStarLevel(), new BigDecimal("0.6"));
				newCre = BigDecimalUtil.add(newCre, shop.getShopCredibility());
				if(shop.getShopCredibility() != null){
					newCre = BigDecimalUtil.div(newCre, new BigDecimal("2"), 2);
				}
				//设置最新的评论信誉度
				sp.setShopCredibility(newCre);
				//设置最新的总信誉度
				sp.setShopAllCredibility(BigDecimalUtil.add(newCre, shop.getShopMoneyCredibility()));
				
				shopService.updateBySelective(sp);
				
				obj.put("status", "y");//状态
				obj.put("info", comment.getCommentId());//返回内容
			}
			return obj;
		} catch (Exception e) {
			logger.error(e);
			return obj;
		}
	}
	
	/**
	 * 卖家中心 - 评论列表展示
	 * @param form
	 * @param map
	 * @return
	 */
	@RequestMapping("/shopCommentList")
	public String shopCommentList(CommentSearchForm form, ModelMap map) {
		try {
			//设置菜单类型
			MenuForm menuForm = new MenuForm(MenuForm.MENU_TYPE_SELLER,MenuForm.PAR_TYPE_SELLER_ORDER,MenuForm.SUB_TYPE_SELLER_ORDER_COMMENT);
			map.addAttribute("menuForm", menuForm);
			
			//设置查询排序
			form.getOrderby().put("commentCreateTime", "desc");
			//创建页面对象
			PageView<Comment> pageView = new PageView<Comment>(3, form.getPage());
			form.setFirstResult(pageView.getFirstResult());
			form.setMaxResult(pageView.getMaxresult());
			pageView.setQueryResult(commentService.getData(form));
			map.addAttribute("pageView", pageView);
			map.addAttribute("form", form);
			map.addAttribute("reqUrl", "/control/order/commentController/shopCommentList.do?commentShopId="+form.getCommentShopId());
			
			return "control/jsp/seller/evaluation";
		} catch(Exception e){
			logger.error(e);
			return "error";
		}
	}
	
	/**
	 * 卖家中心 - 卖家删除用户对对订单的评价
	 * @param form
	 * @param map
	 * @param response
	 */
	@RequestMapping("/shopDeleteComment")
	public void shopDeleteComment(CommentSearchForm form, ModelMap map,HttpServletResponse response) {
		try {
			response.setContentType("text/html");
			PrintWriter out;
			out = response.getWriter();
			Comment o = commentService.find(form.getCommentId());
			if(o != null){
				//删除订单评价为真删除,直接删除数据库记录
				commentService.delete(form.getCommentId());
				out.print("true");
			}else{
				out.print("false");
			}
		} catch (Exception e) {
			logger.error(e);
		}
	}
}
